package com.jinglvhuanjing.app.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.ViewConfiguration;
import android.view.WindowManager;

import androidx.annotation.NonNull;

/**
 * @author Created by AdminFun
 * @version v1.0.0
 * @date 2019/4/15 15:10
 */
public class ScreenUtils {

    /**
     * 状态栏高度
     */
    public static int mStatusBarHeight = 0;

    /**
     * 导航栏/虚拟操作栏的高度
     */
    public static int mNavigationBarHeight = 0;

    /**
     * 根据手机分辨率: dip  转  px
     *
     * @param context 上下文
     * @param dpValue 需要转换的DIP值
     * @return PX
     */
    public static int dip2px(@NonNull Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机分辨率: dip  转  px
     *
     * @param context 上下文
     * @param dpValue 需要转换的DIP值
     * @return PX
     */
    public static float dip2pxFloat(@NonNull Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机分辨率： px  转  dip
     *
     * @param context 上下文
     * @param pxValue 需要转换的PX值
     * @return DIP
     */
    public static int px2dip(@NonNull Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 根据手机分辨率： px  转  sp
     *
     * @param context 上下文
     * @param pxValue 需要转换的PX值
     * @return SP
     */
    public static int px2sp(@NonNull Context context, float pxValue) {
        float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 根据手机分辨率： sp  转 px
     *
     * @param context 上下文
     * @param spValue 需要转换的SP值
     * @return PX
     */
    public static int sp2px(@NonNull Context context, float spValue) {
        float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * scale + 0.5f);
    }

    /**
     * @param mActivity {@link Activity}
     * @return 获取屏幕宽度（px）
     * @deprecated 业务层可以直接获取屏幕宽高，调用方式：EpetService.getDeviceService().getScreenWidth()
     */
    public static int getScreenWidth(@NonNull Activity mActivity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }

    /**
     * @param mActivity {@link Activity}
     * @return 获取屏幕高度（px）
     * @deprecated 业务层可以直接获取屏幕宽高，调用方式：EpetService.getDeviceService().getScreenWidth()
     */
    public static int getScreenHeight(@NonNull Activity mActivity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.heightPixels;
    }

    /**
     * 获取屏幕宽度
     *
     * @deprecated 业务层可以直接获取屏幕宽高，调用方式：EpetService.getDeviceService().getScreenWidth()
     */
    public static int getScreenWidth(@NonNull Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    /**
     * 获取屏幕高度
     *
     * @deprecated 业务层可以直接获取屏幕宽高，调用方式：EpetService.getDeviceService().getScreenWidth()
     */
    public static int getScreenHeight(@NonNull Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }

    public static int getScreenRealHeight(@NonNull Context context) {
        DisplayMetrics outMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getRealMetrics(outMetrics);
        int widthPixel = outMetrics.widthPixels;
        int heightPixel = outMetrics.heightPixels;
        return heightPixel;
    }

    /**
     * 获取状态栏高度
     *
     * @param context 上下文
     * @return 状态栏高度
     */
    public static int getStatusBarHeight(@NonNull Context context) {
        if (ScreenUtils.mStatusBarHeight == 0) {
            int resourceId = context.getResources().getIdentifier(
                    "status_bar_height", "dimen", "android");
            mStatusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }
        return ScreenUtils.mStatusBarHeight;
    }

    /**
     * 是否虚拟操作拦/导航栏（home等）是否显示
     *
     * @param activity Activity
     */
    public static boolean isNavigationBarShow(@NonNull Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Display display = activity.getWindowManager().getDefaultDisplay();
            Point size = new Point();
            Point realSize = new Point();
            display.getSize(size);
            display.getRealSize(realSize);
            return realSize.y != size.y;
        } else {
            boolean menu = ViewConfiguration.get(activity).hasPermanentMenuKey();
            boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
            if (menu || back) {
                return false;
            } else {
                return true;
            }
        }
    }

    /**
     * 获取虚拟操作拦（home等）高度
     */
    public static int getNavigationBarHeight(@NonNull Activity activity) {
        if (!isNavigationBarShow(activity)) {
            return 0;
        }
        if (mNavigationBarHeight == 0) {
            int resourceId = activity.getResources().getIdentifier(
                    "navigation_bar_height", "dimen", "android");
            mNavigationBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
        }
        return mNavigationBarHeight;
    }

    /**
     * 当前系统版本是否支持沉浸状态栏
     */
    public static boolean isSupportImmerseStatusBar() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
    }

}